package com.jfok.server.login;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import com.jfok.server.common.ActionResult;
import com.jfok.server.common.ILoginService;
import com.jfok.server.common.LoginResult;
import com.jfok.server.service.SystemGlobalInfoService;

@Service
public class LoginService implements ILoginService {

	Log log = LogFactory.getLog(LoginService.class);

	@Resource
	private LoginLogic loginLogic;

	@Override
	@Transactional(propagation = Propagation.REQUIRED)
	public ActionResult login(String loginname, String base64password, String identifyingCode,
			HttpServletRequest request) {
		log.info("用户登录:" + loginname);
		LoginResult result = loginLogic.login(LoginType.USERLOGIN, loginname, base64password,
				identifyingCode, request);
		log.info("登录验证:" + result.getContext());
		ActionResult actionResult = new ActionResult();
		if (result != LoginResult.LOGINOK) {
			actionResult.setSuccess(false);
			actionResult.setMsg(result.getContext());
		}
		return actionResult;
	}

	@Override
	@Transactional(propagation = Propagation.REQUIRED)
	public void logout(HttpServletRequest request, HttpServletResponse response) {
		log.info("用户注销");
		loginLogic.userLogout(request, response);
		request.getSession().invalidate();
	}

	@Override
	public String forgetPassword() {
		return SystemGlobalInfoService.getSystemsetAddition().getTf_userforgetPassword();
	}

}
